<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/11/21
  Time: 12:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-L-admin1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/static/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->


</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">演示</a>
        <a>
          <cite>导航元素</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:38px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so" id="f1"
              action="${pageContext.request.contextPath}/product/findProductPlanAll.do">
            <%--              ${status}为生产计划列表状态              --%>
        </form>
    </div>
    <xblock>
        <c:if test="${status == 0}">
            <button class="layui-btn layui-btn-danger" onclick="delAll(${productId},${status},${productPlanId})"><i
                    class="layui-icon"></i>批量删除
            </button>
            <button class="layui-btn" onclick="addProductMaterial(${productId},${status},${productPlanId})"><i
                    class="layui-icon"></i>添加产品原料
            </button>
            <button class="layui-btn layui-bg-blue" onclick="toProduct(${productId},${productPlanId},${productPlanId})">
                <i
                        class="layui-icon"></i>投入生产
            </button>
            <%--            <button class="layui-btn layui-bg-orange" onclick="updateMaterialInProduct(${productId},${productPlanId})">--%>
            <%--                <i--%>
            <%--                        class="layui-icon"></i>修改所需原料--%>
            <%--            </button>--%>

        </c:if>
        <%--        <c:if  test="${status == 1}">--%>

        <%--        </c:if>--%>

    </xblock>

    <table class="layui-table">
        <thead>
        <tr>
            <th>
                <div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i
                        class="layui-icon">&#xe605;</i></div>
            </th>
            <th>原料产品编号</th>
            <th>所需原料名称</th>
            <th>所需原料数量</th>
            <th>所需原料单位</th>
            <th>状态</th>
            <th>操作</th>
        </thead>
        <tbody>
        <c:forEach var="findMaterial" items="${Page}">
            <tr>
                <td>
                    <div class="layui-unselect layui-form-checkbox" lay-skin="primary"
                         data-id='${findMaterial.materialProductId}'><i
                            class="layui-icon">&#xe605;</i></div>
                </td>
                <td>${findMaterial.materialProductId}</td>
                <td>${findMaterial.materialName}</td>
                <td>${findMaterial.materialQuantity}</td>
                <td>${findMaterial.unit}</td>
                <c:choose>
                    <c:when test="${findMaterial.status == 0}">
                        <td>未可投入生产</td>
                    </c:when>
                    <c:when test="${findMaterial.status == 1}">
                        <td>可投入生产</td>
                    </c:when>
                </c:choose>
                <td class="td-manage">
                    <c:if test="${findMaterial.status == 0}">

                        <%--                    <c:if test="${}">--%>
                        <%--                        --%>
                        <%--                    </c:if>--%>
                        <a onclick="certainProductMaterial(${findMaterial.materialProductId},${findMaterial.productId},
                            ${findMaterial.materialId},${findMaterial.materialQuantity},
                            ${quantity},${status},${productPlanId})"
                           class="layui-btn layui-btn-sm layui-btn-primary"
                           href="javascript:;" title="确定">
                            确定
                        </a>
                        <a title="修改产品原料" class="layui-btn layui-btn-sm layui-btn-normal"
                           onclick="updateProductMaterial(${findMaterial.materialProductId},${status})"
                           href="javascript:;">
                            修改产品原料
                        </a>
                        <%--                    <a class="layui-btn layui-btn-sm layui-btn-warm"--%>
                        <%--                       onclick="x_admin_show('修改密码','member-password.html',600,400)" title="修改密码" href="javascript:;">--%>
                        <%--                        修改密码--%>
                        <%--                    </a>--%>
                        <%--                    <a title="删除" class="layui-btn layui-btn-sm layui-btn-danger" onclick="member_del(this,'要删除的id')"--%>
                        <%--                       href="javascript:;">--%>
                        <%--                        删除--%>
                        <%--                    </a>--%>
                    </c:if>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>

    <%--        <div class="page">--%>
    <%--            <div>--%>
    <%--                <a href="javascript:go(1)" class="num">首页</a>--%>
    <%--                <c:if test="${Page.hasPreviousPage}">--%>
    <%--                    <a class="prev" href="javascript:go('${Page.prePage}')">&lt;&lt;</a>--%>
    <%--                </c:if>--%>
    <%--                <c:choose>--%>
    <%--                    <c:when test="${Page.pages<=10}">--%>
    <%--                        <c:set var="begin" value="1"></c:set>--%>
    <%--                        <c:set var="end" value="${Page.pages}"></c:set>--%>
    <%--                    </c:when>--%>
    <%--                    <c:otherwise>--%>
    <%--                        <c:set var="begin" value="${Page.pageNum-5}"></c:set>--%>
    <%--                        <c:set var="end" value="${Page.pageNum+4}"></c:set>--%>
    <%--                        <c:if test="${begin<1}">--%>
    <%--                            <c:set var="begin" value="1"></c:set>--%>
    <%--                            <c:set var="end" value="10"></c:set>--%>
    <%--                        </c:if>--%>
    <%--                        <c:if test="${end>Page.pages}">--%>
    <%--                            <c:set var="begin" value="${Page.pages-9}"></c:set>--%>
    <%--                            <c:set var="end" value="${Page.pages}"></c:set>--%>
    <%--                        </c:if>--%>
    <%--                    </c:otherwise>--%>
    <%--                </c:choose>--%>
    <%--                <c:forEach var="i" begin="${begin}" end="${end}">--%>
    <%--                    <c:choose>--%>
    <%--                        <c:when test="${Page.pageNum eq i}">--%>
    <%--                            <span class="current">${i}</span>--%>
    <%--                        </c:when>--%>
    <%--                        <c:otherwise>--%>
    <%--                            <a class="num" href="javascript:go(${i})">${i}</a>--%>
    <%--                        </c:otherwise>--%>
    <%--                    </c:choose>--%>
    <%--                </c:forEach>--%>
    <%--                &lt;%&ndash;            &ndash;%&gt;--%>
    <%--                &lt;%&ndash;            <a class="num" href="">3</a>&ndash;%&gt;--%>
    <%--                &lt;%&ndash;            <a class="num" href="">489</a>&ndash;%&gt;--%>
    <%--                <c:if test="${Page.hasNextPage}">--%>
    <%--                    <a class="next" href="javascript:go('${Page.nextPage}')">&gt;&gt;</a>--%>
    <%--                </c:if>--%>
    <%--                <a href="javascript:go(${Page.pages})" class="num">尾页</a>--%>
    <%--            </div>--%>
    <%--        </div>--%>

</div>

<script>

    $("#sel").change(function () {
        $("#pageSize").val($("#sel").val())
        $("#pageNum").val(1)
        $("#f1").submit()
    })

    function go(pageNum) {
        $("#pageSize").val($("#sel").val())
        $("#pageNum").val(pageNum)
        $("#f1").submit()
    }

    function delAll(id, status, productPlanId) {
        var data = tableCheck.getData();

        if (data.length > 0) {
            layer.confirm('确认要删除吗？' + data, function (index) {
                var ids = []
                for (var i = 0; i < data.length; i++) {
                    ids.push(data[i])
                }
                window.location.href = "${pageContext.request.contextPath}/product/delProductMaterial.do?ids=" + ids + "&productId=" + id + "&status=" + status + "&productPlanId=" + productPlanId;
                layer.msg('删除成功', {icon: 200});
            });

        } else {
            layer.confirm('必须选择一个删除')
        }

    }


    function toFindMaterial(id, pageNum) {
        window.location.href = "${pageContext.request.contextPath}/product/toFindMaterial.do?pageNum=" + pageNum + "&productId=" + id;
    }

    function addProductMaterial(id, status, productPlanId) {
        window.location.href = "${pageContext.request.contextPath}/product/findMaterialList.do?productId=" + id + "&status=" + status + "&productPlanId=" + productPlanId;
    }

    function updateProductMaterial(pmid, status) {
        window.location.href = "${pageContext.request.contextPath}/product/updateProductMaterial.do?materialProductId=" + pmid + "&status=" + status;
    }

    function certainProductMaterial(pid, proid, id, quantity, quantity2, status, productPlanId) {
        var ajax = new XMLHttpRequest();
        ajax.open("get", "${pageContext.request.contextPath}/product/findGetMaterialQuantity.do?materialId=" + id + "&materialQuantity=" + quantity + "&quantity=" + quantity2, true);

        ajax.send(null);

        ajax.onreadystatechange = function () {

            if (ajax.readyState === 4) {

                var str = ajax.responseText;

                if (str == "OK") {
                    quantity = quantity * quantity2;
                    window.location.href = "${pageContext.request.contextPath}/product/certainProductMaterial.do?materialId=" + id + "&materialQuantity="
                        + quantity + "&materialProductId=" + pid + "&productId=" + proid + "&status=" + status + "&productPlanId=" + productPlanId;
                } else if (str == "ERROR") {
                    alert("原料不足,请联系库存提取原料!")
                }
            }
        }
    }

    function toProduct(id, pid, productPlanId) {
        var ajax = new XMLHttpRequest();
        ajax.open("get", "${pageContext.request.contextPath}/product/checkProduct.do?productId=" + id + "&productPlanId=" + productPlanId, true);

        ajax.send(null);

        ajax.onreadystatechange = function () {

            if (ajax.readyState === 4) {

                var str = ajax.responseText;

                if (str == "OK") {
                    window.location.href = "${pageContext.request.contextPath}/product/toProduct.do?productPlanId=" + pid
                } else if (str == "ERROR") {
                    alert("无法生产,请检查原料状态和数量!")
                }
            }
        }
    }
</script>
</body>

</html>
